'use strict';

const nums = [1, 2, 0, 1];

function longestConsecutive(nums) {
  if (nums.length === 0) return 0;

  let numSet = new Set(nums);
  let maxLen = 0;

  for (let num of numSet) {
    if (!numSet.has(num - 1)) {
      // 只有当当前数字是序列的起点才进行处理
      let currentNum = num;
      let currentLen = 1;

      while (numSet.has(currentNum + 1)) {
        currentNum++;
        currentLen++;
      }

      maxLen = Math.max(maxLen, currentLen);
    }
  }

  return maxLen;
}

console.log(longestConsecutive(nums));
